// index.js
import {
  carList,
  storeList
} from "../../api/index/index"
const app = getApp()
Page({
  data: {

    list: [{
        url: '/assets/home (1).png',
        text: '玻璃安检',
        page: '/pages/store/index'
      },
      {
        url: '/assets/home (2).png',
        text: '车险试算',
        page: ''
      },
      {
        url: '/assets/home (3).png',
        text: '电池预警',
        page: ''
      },
      {
        url: '/assets/home (4).png',
        text: '电池维保',
        page: ''
      },
      {
        url: '/assets/home (5).png',
        text: '电池回收',
        page: ''
      },
      {
        url: '/assets/home (6).png',
        text: '周边充电',
        page: ''
      },
      {
        url: '/assets/home (7).png',
        text: '理赔管家',
        page: ''
      },
      {
        url: '/assets/home (8).png',
        text: '特价洗车',
        page: ''
      },
    ],
    equityList: [{
        url: '/assets/yell.png',
        title: '5五张免费标准洗车券',
        price: '109',
        oldPrice: '140',
        discountPrice: '41'
      },
      {
        url: '/assets/green.png',
        title: '3张7元洗车立减券',
        price: '109',
        oldPrice: '140',
        discountPrice: '41'
      },
      {
        url: '/assets/prop.png',
        title: '3张7元洗车立减券',
        price: '109',
        oldPrice: '140',
        discountPrice: '41'
      },
    ],
    index: 1,
    show: false,
    storeArray: [],
    isDefault: false
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onMore() {
    wx.navigateTo({
      url: '/pages/store/index',
    })
  },
  handleCilck(params) {
    console.log(params)
    this.setData({
      index: params.target.dataset.type
    })
  },
  handleCilckIcon(e) {
    const page = e.currentTarget.dataset.type
    if (page) {
      wx.navigateTo({
        url: page,
      })
    }
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    const {
      nickName
    } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const {
      avatarUrl
    } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onAddCar() {

    if (this.data.carNo) {
      console.log(app.globalData.isHaveCar.id)
      const id = app.globalData.isHaveCar.id
      wx.navigateTo({
        url: '/pages/addCar/addFrom/index?ids=' + id
      })
    } else {
      wx.navigateTo({
        url: '/pages/addCar/addFrom/index',
      })
    }


  },

  getCarList() {
    let that = this
    carList().then(res => {
      console.log(res)
      if (!res.data.data.carNo) {
        this.setData({
          show: true
        })

      } else {
        this.setData({
          isDefault: true,
          logo: res.data.data.carMark,
          name: res.data.data.carBrand,
          carNo: res.data.data.carNo
        })
        app.globalData.isHaveCar = res.data.data
        wx.getLocation({
          type: 'wgs84',
          success(res) {
            console.log(res)
            const latitude = res.latitude
            const longitude = res.longitude
            const speed = res.speed
            const accuracy = res.accuracy
            that.getStoreList(latitude, longitude)
          }
        })
      }
    })
  },
  getStoreList(latitude, longitude) {
    storeList({
      lat: latitude,
      lng: longitude
    }).then(res => {
      console.log(res)
      this.setData({
        storeArray: res.data.data.map(item => {
          item.dis = (item.distance / 1000).toFixed(2),
            item.name = item.name,
            item.address = item.address
          return item
        })
      })
    })
  },
  onLoad(options) {
    console.log(options)
    this.getCarList()
  },
  onShow(options) {
    console.log(options)
  },
})